home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / lds / lds.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  418 lines

  1. /*************************************************************************
  2.  *                                                                       *
  3.  *  Copyright (c) 1988-1993 Ronald Joe Record                           *
  4.  *                                                                       *
  5.  *  All rights reserved. No part of this program or publication may be   *
  6.  *  reproduced, transmitted, transcribed, stored in a retrieval system,  *
  7.  *  or translated into any language or computer language, in any form or *
  8.  *  by any means, electronic, mechanical, magnetic, optical, chemical,   *
  9.  *  biological, or otherwise, without the prior written permission of:   *
  10.  *                                                                       *
  11.  *      Ronald Joe Record (408) 458-3718                                 *
  12.  *      212 Owen St., Santa Cruz, California 95062 USA                   *
  13.  *                                                                       *
  14.  *************************************************************************/
  15.  
  16. /*
  17.  *    Written 2/22/88 by Ronald Record (sco!rr) in partial fulfillment of
  18.  *    the Mathematics Ph.D. requirements at the University of California at 
  19.  *    Santa Cruz. This work is in part based upon articles by Doyne Farmer, 
  20.  *    Jim Crutchfield and Kunihiko Kaneko.
  21.  *
  22.  *  X11 port begun 20 Jan 93 by Ronald Record (rr@sco.com) for fun
  23.  */
  24.  
  25. #include <math.h>
  26. #include <memory.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <values.h>
  30. #include "x.h"
  31. #include "defines.h"
  32. #include "lds.h"
  33.  
  34. /* routines in this file */
  35. void usage(), Clear();
  36. void redisplay(), BufferSpace(), FlushHisbuf();
  37. void resize(), event_loop(), freemem(), zero_mem();
  38. void spahist(), hist(), adjconn(), zerohist(), zerospa();
  39. int complds(), upd_lambda();
  40. double map();
  41.  
  42. /* external routines called in this file */
  43. extern void Getkey(), draw_hist(), init_mem(), init_gen(), next_gen();
  44. extern void InitBuffer();
  45.  
  46. void
  47. usage()
  48. {
  49.   printf("\nUsage: lds [-PSpAcdx] [-w wide] [-h high] [-n lambda] [-b#]\n");
  50.   printf("\t\t[-f#] [-F freq] [-C EC] [-L EL] [-R ER] [-U EU] [-D ED]\n");
  51.   printf("\t\t[-W width] [-H height] [-r range] [-M omega] [-T tall] [-E rate] [-i init]\n");
  52.     printf("Where :\n");
  53.     printf("\t'-A' creates site histogram curve\n");
  54.     printf("\t'-p' indicates draw phase 1st differences\n");
  55.     printf("\t'-c' indicates draw curves\n");
  56.     printf("\t'-d' indicates demo mode (use with -f# option)\n");
  57.     printf("\t'-x' indicates complex dynamical system mode\n");
  58.     printf("\t'-P' indicates draw points (default is curves)\n");
  59.     printf("\t'-[M|T] #' indicates use circle|tent map with omega|height=#\n");
  60.     printf("\t'-E #' indicates evolve connection strengths at rate #\n");
  61.     printf("\tEC|EL|ER|EU|ED indicates weight of ctr|lft|rgt|upr|lwr cell\n");
  62.     printf("\twidth\t= width of window (default is full screen)\n");
  63.     printf("\theight\t= height of window (default is full screen)\n");
  64.     printf("\twide\t= width of lattice (default is full screen)\n");
  65.     printf("\thigh\t= height of lattice (default is full screen)\n");
  66.     printf("\tinit\t= initial condition (default is random)\n");
  67.     printf("\tlambda\t= logistic parameter (default is 3.7)\n");
  68.     printf("\t-b#\t= first generation graphed (default is 1)\n");
  69.     printf("\t-f#\t= Final generation graphed (default is MAXSHORT)\n");
  70.     printf("\tfreq\t= display every freq'th generation (1 is default)\n");
  71.     printf("\trange\t= range outside of which differences are graphed\n");
  72.     printf("During display, use of the keys 'cseEdBmHPLSXq' indicates:\n");
  73.     printf("'continue' 'single step' 'erase' 'Erase all' 'draw' 'Begin again' 'multi-step'\n");
  74.     printf("'Histogram' 'Points' 'Lines' 'Spin one color' 'Xtended spin' 'quit'\n");
  75.         exit(1);
  76. }
  77.  
  78. void
  79. Clear(w) 
  80. Window w;
  81. {
  82.     if (w == canvas) {
  83.         XFillRectangle(dpy, pixmap, Data_GC[0], 0, 0, width, height);
  84.         XCopyArea(dpy, pixmap, w, Data_GC[0], 0, 0, width, height, 0, 0);
  85.         InitBuffer(&Points, numcolors);
  86.     }
  87.     else if (w == spahis) {
  88.         XFillRectangle(dpy, spamap, Data_GC[0], 0, 0, width, height);
  89.         XCopyArea(dpy, spamap, w, Data_GC[0], 0, 0, width, height, 0, 0);
  90.         InitBuffer(&Points, numcolors);
  91.     }
  92.     else
  93.         XClearWindow(dpy, w);
  94. }
  95.  
  96. void
  97. BufferSpace(color, x, y)
  98. int color;
  99. int x, y;
  100. {
  101.     if (Hpoints.npoints[color] == MAXPOINTS)
  102.     {
  103.         XDrawPoints(dpy, spahis, Data_GC[color],
  104.             Hpoints.data[color], Hpoints.npoints[color], CoordModeOrigin);
  105.         XDrawPoints(dpy, spamap, Data_GC[color],
  106.             Hpoints.data[color], Hpoints.npoints[color], CoordModeOrigin);
  107.         Hpoints.npoints[color] = 0;
  108.     }
  109.     Hpoints.data[color][Hpoints.npoints[color]].x = x;
  110.     Hpoints.data[color][Hpoints.npoints[color]].y = height - y;
  111.     ++Hpoints.npoints[color];
  112. }
  113.  
  114. void
  115. FlushHisbuf()
  116. {
  117.     int color;
  118.     for (color = mincolor; color < numcolors; ++color)
  119.         if (Hpoints.npoints[color])
  120.         {
  121.             XDrawPoints(dpy, spahis, Data_GC[color],
  122.                 Hpoints.data[color], Hpoints.npoints[color], CoordModeOrigin);
  123.             XDrawPoints(dpy, spamap, Data_GC[color],
  124.                 Hpoints.data[color], Hpoints.npoints[color], CoordModeOrigin);
  125.             Hpoints.npoints[color] = 0;
  126.         }
  127. }
  128.  
  129. void
  130. redisplay(ev, w)
  131. XExposeEvent    *ev;
  132. Window w;
  133. {
  134.     /*
  135.      * Extract the exposed area from the event and copy
  136.      * from the saved pixmap to the window.
  137.      */
  138.     if (w == canvas)
  139.         XCopyArea(dpy, pixmap, canvas, Data_GC[0], 
  140.            ev->x, ev->y, ev->width, ev->height, ev->x, ev->y);
  141.     else if (w == spahis)
  142.         XCopyArea(dpy, spamap, spahis, Data_GC[0], 
  143.            ev->x, ev->y, ev->width, ev->height, ev->x, ev->y);
  144. }
  145.  
  146. void
  147. resize(w)
  148. Window w;
  149. {
  150.     Window r;
  151.     int x, y;
  152.     unsigned int bw, d, new_w, new_h;
  153.     static XWindowChanges values;
  154.  
  155.     XGetGeometry(dpy,w,&r,&x,&y,&new_w,&new_h,&bw,&d);
  156.     if ((new_w == width) && (new_h == height))
  157.         return;
  158.     freemem();
  159.     width = new_w; height = new_h;
  160.     XClearWindow(dpy, canvas);
  161.     if (sflag)
  162.         XClearWindow(dpy, spahis);
  163.     if (hflag)
  164.         XClearWindow(dpy, hiswin);
  165.     if (pixmap)
  166.         XFreePixmap(dpy, pixmap);
  167.     pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), 
  168.             width, height, DefaultDepth(dpy, screen));
  169.     values.width = width;
  170.     values.height = height;
  171.     if (w == canvas) {
  172.         XConfigureWindow(dpy,hiswin,CWWidth|CWHeight,&values);
  173.         XConfigureWindow(dpy,spahis,CWWidth|CWHeight,&values);
  174.     }
  175.     else if (w == hiswin) {
  176.         XConfigureWindow(dpy,canvas,CWWidth|CWHeight,&values);
  177.         XConfigureWindow(dpy,spahis,CWWidth|CWHeight,&values);
  178.     }
  179.     else {
  180.         XConfigureWindow(dpy,canvas,CWWidth|CWHeight,&values);
  181.         XConfigureWindow(dpy,hiswin,CWWidth|CWHeight,&values);
  182.     }
  183.     xpoint = ypoint = 0;
  184.     run = 1;
  185.     init_mem();
  186.     InitBuffer(&Points, numcolors);
  187.     Clear(canvas);
  188.     init_gen();
  189. }
  190.  
  191. int
  192. complds()
  193. {
  194.     if (!run)
  195.         return(TRUE);
  196.     next_gen();
  197.     if (hflag)
  198.         draw_hist();
  199.     return(FALSE);
  200. }
  201.  
  202. void
  203. event_loop()
  204. {
  205.     int n;
  206.     XEvent event;
  207.  
  208.     if (complds() == TRUE)
  209.         run=0;
  210.     n = XEventsQueued(dpy, QueuedAfterFlush);
  211.     while (n--) {
  212.         XNextEvent(dpy, &event);
  213.         switch(event.type) 
  214.         {
  215.         case KeyPress:
  216.             Getkey(&event);
  217.             break;
  218.         case Expose:
  219.             redisplay(&event, event.xexpose.window);
  220.                break;
  221.         case ConfigureNotify:
  222.             resize(event.xconfigure.window);
  223.               break;
  224.         }
  225.     }
  226. }
  227.  
  228. void
  229. freemem()
  230. {
  231.     static int i;
  232.     extern XPoint *hpoints;
  233.  
  234.     for (i=0; i<width; i++)
  235.         free(histarray[i]);
  236.     for (i=0; i<high; i++) {
  237.         free(lambda[i]);
  238.         free(lftconn[i]);
  239.         free(rgtconn[i]);
  240.         free(ctrconn[i]);
  241.         free(uprconn[i]);
  242.         free(lwrconn[i]);
  243.         free(currentgen[i]);
  244.         free(nextgen[i]);
  245.         free(avg[i]);
  246.     }
  247.     free(lambda);
  248.     free(lftconn);
  249.     free(rgtconn);
  250.     free(ctrconn);
  251.     free(uprconn);
  252.     free(lwrconn);
  253.     free(currentgen);
  254.     free(nextgen);
  255.     free(histogram);
  256.     free(histarray);
  257.     free(diff); 
  258.     free(avg);
  259.     free(hpoints);
  260. }
  261.  
  262. void
  263. zerospa()
  264. {
  265.     static int i, j;
  266.  
  267.     for (i=0; i<width; i++)
  268.         for (j=0; j<height; j++)
  269.             histarray[i][j] = 0;
  270. }
  271.  
  272. void
  273. zerohist()
  274. {
  275.     static int i;
  276.  
  277.     for (i=0; i<width; i++)
  278.         histogram[i] = 0;
  279. }
  280.  
  281. void
  282. zero_mem()
  283. {
  284.     static int i, j;
  285.  
  286.     for (i=0; i<wide; i++) {
  287.         diff[i] = 0.0;
  288.         for (j=0; j<high; j++) {
  289.             lambda[j][i] = 0.0;
  290.             lftconn[j][i] = 0.0;
  291.             rgtconn[j][i] = 0.0;
  292.             ctrconn[j][i] = 0.0;
  293.             currentgen[j][i] = 0.0;
  294.             nextgen[j][i] = 0.0;
  295.             avg[j][i] = 0.0;
  296.         }
  297.         zerohist();
  298.     }
  299. }
  300.  
  301. int
  302. upd_lambda() 
  303. {
  304.     static int i, j;
  305.  
  306.     if (xavg)
  307.       if (numgen % xavg) {
  308.         for (i=0;i<wide;i++)
  309.           for (j=0;j<high;j++)
  310.             avg[j][i] += currentgen[j][i];
  311.         return(1);
  312.       }
  313.     for (i=0;i<wide;i++)
  314.       for (j=0;j<high;j++)
  315.         avg[j][i] = (avg[j][i] + (double)currentgen[j][i]) / (double)xavg;
  316.     for (i=0;i<high;i++) {
  317.       if (circle)
  318.         lambda[i][0] = avg[i][wide-1]/M_2PI;
  319.       else if (tent)
  320.         lambda[i][0] = avg[i][wide-1];
  321.       else 
  322.         lambda[i][0] = 3.0 + avg[i][wide-1];
  323.     }
  324.     for (i=1;i<wide;i++)
  325.       for (j=0;j<high;j++) {
  326.         if (circle)
  327.             lambda[j][i] = avg[j][i-1]/M_2PI;
  328.         else if (tent)
  329.             lambda[j][i] = avg[j][i-1];
  330.         else 
  331.             lambda[j][i] = 3.0 + avg[j][i-1];
  332.       }
  333.     return(0);
  334. }
  335.  
  336. void
  337. spahist(sites)
  338. double *sites;
  339. {
  340.     static int i, j, k, l, n;
  341.  
  342.     for (i=0;i<wide;i++) {
  343.         j = (int)(sites[i]*height);
  344.         k = (i*width)/wide;
  345.         n = ++histarray[k][j];
  346.         if (n > maxhist)
  347.             maxhist = n;
  348.         if (n < minhist)
  349.             minhist = n;
  350.         l = ((histarray[k][j]-minhist)*(numcolors-mincolor-1)/maxhist)+mincolor;
  351.         BufferSpace(l, k, j);
  352.     }
  353. }
  354.  
  355. void
  356. hist(sites)
  357. double *sites;
  358. {
  359.     int i;
  360.  
  361.     for (i=0;i<wide;i++)
  362.         histogram[(int)floor(sites[i]*width)]++;
  363. }
  364.  
  365. void
  366. adjconn()
  367. {
  368.     static int i,j,k,l,m,n;
  369.  
  370.     for (i=0;i<wide;i++) {
  371.         if (i == wide -1)
  372.             k = 0;
  373.         else
  374.             k = i + 1;
  375.         if (i == 0)
  376.             l = wide - 1;
  377.         else
  378.             l = i - 1;
  379.         for (j=0;j<high;j++) {
  380.             if (j == high -1)
  381.                 m = 0;
  382.             else
  383.                 m = j + 1;
  384.             if (j == 0)
  385.                 n = high - 1;
  386.             else
  387.                 n = j - 1;
  388.             rgtconn[j][i]=erate*(currentgen[j][i]*currentgen[j][k]);
  389.             lftconn[j][i]=erate*(currentgen[j][i]*currentgen[j][l]);
  390.             uprconn[j][i]=erate*(currentgen[j][i]*currentgen[m][i]);
  391.             lwrconn[j][i]=erate*(currentgen[j][i]*currentgen[n][i]);
  392.             ctrconn[j][i] = 1.0 - 
  393.                 (rgtconn[j][i] + lftconn[j][i] + uprconn[j][i] + lwrconn[j][i]);
  394.         }
  395.     }
  396. }
  397.  
  398. double
  399. map(npar,x)
  400. double npar;
  401. double x;
  402. {
  403.     double y;
  404.  
  405.     x = fmod(x,1.0);    /*    Make sure value is between 0 and 1 */
  406.     if (logistic)
  407.         y = npar * x * (1.0 - x);
  408.     else if (circle)
  409.         y = omega + x - npar * sin(M_2PI*x);
  410.     else if (tent)
  411.         if (x < npar)
  412.             y = incline*x;
  413.         else
  414.             y = decline*(x-1.0);
  415.     y = fmod(y,1.0);    /*    Make sure return value is between 0 and 1 */
  416.     return(y);
  417. }
  418.